function createCookie(name,value,days) 
{
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

$(function()
{
	var pause 		= 1000,
		year 		= $('#year'),
		thanks		= $('#thanks'),
		hideYear	= readCookie('hideYear');

	if (!hideYear){
		setTimeout(
			function(){
				year.animate({bottom: '0'}, 700);
			}, pause
		);
		
		$('a#hide').click(
			function()
			{
				year.animate({bottom: '-200px'}, 200);
				return false;
			}
		);
		
		$('a#kill').click(
			function()
			{
				createCookie( 'hideYear', 0, 365 );
				year.animate({bottom: '-200px'}, 200);
				return false;
			}
		);
		
		$('#register').click(
			function()
			{
				var nome 	= $('#name').val(),
					mail 	= $('#emailbanner').val(),
					error	= false,
					messaggio = '',
					filter 	= /^[A-Z0-9+_.-]+@[A-Z0-9.-]+$/i;

				if( !nome ){
					error = true;
					messaggio = 'First name is required\n';
				}
				else{
					messaggio = '';
				}
				
				if( !filter.test(mail) ){
					error = true;
					messaggio += 'Please, enter a valid email address';
				}
				
				if( error ){
					alert(messaggio);
				}
				else{
				
					year.animate({bottom: '-200px'}, 200);
					thanks.animate({bottom: '0'}, 700);
					//createCookie( 'hideYear', 0, 365 );
					
					$.post(
						'/list/?p=subscribe&id=5', 
						{email: mail, attribute2: name, htmlemail: '1', 'list[3]': 'signup', 'listname[3]': 'Your Inspiration Web en', subscribe: 'Subscribe'}
					);	
				}
				
				return false;
				
			}
		);
		
	}
	
});
